# 处理数据
from db import db_hander
from db.db_hander import *
from core.src import *


def register_interface(username, userpasswd, t_userpasswd):
    '''
    src注册接口
    :param username:
    :param userpasswd:
    :param t_userpasswd:
    :return:
    '''
    if userpasswd != t_userpasswd:
        return False, "两次密码不一样"
    user_obj = search(username)
    if user_obj:
        return False, "用户已经存在"
    else:
        user_info = {
            "username": username,
            "userpasswd": userpasswd,
            "money":1000000
        }
        save(user_info)
        return True, "注册成功"


def login_interface(username, userpasswd):
    '''
    src 登入接口
    :param username:
    :param userpasswd:
    :return:
    '''

    if search(username):
        data = search(username)
        if userpasswd == data["userpasswd"]:
            return True, "登入成功"
        else:
            return False, "密码错误！"
    else:
        return False, "用户不存在"
def shoppingthing_interface():
    date = select_comm()
    print(date)

def shopping_interface(username,commname):
    date = select_comm()
    user = search(username)
    if commname in date:
        user["money"] -= int(date[commname])
        shopping_info= {
            "username": user["username"],
            "shoppingname": commname,
            "money": int(date[commname])
        }
        save_shoping(shopping_info)
        return True, "购物成功"
    else:
        return False,"商品不存在"

def transmoney_interface(username , transname):
    user = search(username)
    transname = search(transname)
    if not transname:
        return False,"用户不存在"
    else:
        money = input("输入转账金额")
        if user["money"] > int(money):
            user["money"] -= int(money)
            transname["money"] -= int(money)
            return True, "转账成功"
        else:
            return False,"余额不足"
def shopps_interface(usesrname):
    date = select_shopping(usesrname)
    print(date)